package com.example.arffrecorder.graphics;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import com.example.arffrecoder.services.ARFFLocationService;
import com.example.arffrecoder.services.ARFFLocationService.LocalBinder;
import com.example.arffrecorder.R;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.graphics.Canvas;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Parcelable;
import android.speech.tts.TextToSpeech;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.Toast;

public class VisualizeActivity extends Activity {
    RoomVisualization roomView;
    private String roomState = "empty";
	boolean serviceBound = false;
	private boolean serviceStarted = false;
	ArrayList<String> offlineUsers = new ArrayList<String>();
	ArrayList<String> onlineIds = new ArrayList<String>();
	ArrayList<String> onlineRoles = new ArrayList<String>();

	private Intent intent;
	ARFFLocationService mService;

       
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		intent = new Intent(this, ARFFLocationService.class);

        // Set full screen view
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                         WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //create a new RoomVisualization  view
        roomView = new RoomVisualization(this);
        //set the view as the view of the activity
        setContentView(roomView);
        roomView.requestFocus();
        //bind to the ARFFLocationService
        if (serviceBound == false) {
			serviceBound = true;
			
			
			bindService(intent, mConnection, Context.BIND_AUTO_CREATE);		
			//register a receiver to the data sent by the service to the activity
			registerReceiver(broadcastReceiver, new IntentFilter(ARFFLocationService.BROADCAST_ACTION));
		}
        //roomView.s
    }
    @Override
	protected void onStart() {
		super.onStart();
		
	}
    protected void onResume() {
		super.onResume();
		System.out.println("resume" + serviceBound);
		if (serviceBound){
			bindService(intent, mConnection, Context.BIND_AUTO_CREATE);	
			registerReceiver(broadcastReceiver, new IntentFilter(ARFFLocationService.BROADCAST_ACTION));
		}
	}

	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onStop() {
		
		super.onStop();
		if (isFinishing() && serviceBound){
			unbindService(mConnection);
			serviceBound = false;
			unregisterReceiver(broadcastReceiver);
			//stopService(intent);
		}
	}

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
    
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.visualize_menu, menu);
		return true;
	}
	/**
	 * actions to be taken if one item from the menu is chosen
	 */
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.back:

			
			return true;
		
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	/*
	 * create a new connection to the service
	 */
	private ServiceConnection mConnection = new ServiceConnection() {
		@Override
		public void onServiceConnected(ComponentName className, IBinder service) {
			// We've bound to LocalService, cast the IBinder and get
			// LocalService instance
			LocalBinder binder = (LocalBinder) service;
			mService = binder.getService();
			//myMessenger = new Messenger (service);
		}
		@Override
		public void onServiceDisconnected(ComponentName arg0) {
			serviceBound = false;
		}
	
	};
	/**receive information from the service (raw_data: acceleration values or label of the activity)
	 * initializet the room state label and the local arrays containing users' data
	 */
	private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
        	String info_received = intent.getStringExtra("message_type");

        	if (info_received.equals("show_room")){
        		String room = intent.getStringExtra("room_state");
        		offlineUsers = intent.getStringArrayListExtra("offline_users");
        		onlineIds = intent.getStringArrayListExtra("onlineIds");
        		onlineRoles = intent.getStringArrayListExtra("onlineRoles");
        		System.out.println("gagauta"+onlineIds.size()+"/"+onlineRoles.size());
        		if (!(roomState.equals(room))){      			
        			roomState = room;
        		}
    			changeRoom(roomState);

        	}
        }
    }; 
    /*
     * update the view according to the room state and the state of the users
     */
    public void changeRoom(String roomState){
    	RoomVisualization.offlineUsers = offlineUsers;
    	RoomVisualization.onlineRoles = onlineRoles;
    	RoomVisualization.onlineUsers = onlineIds;
    	if (roomState.equals("empty")){
    		roomView.invalidate();
			RoomVisualization.roomState = 1;
			roomView.draw(new Canvas());
    	}
    	else {
    		if (roomState.equals("transition")){
    			roomView.invalidate();
    			RoomVisualization.roomState = 2;
    			roomView.draw(new Canvas());
    		}
    		else if (roomState.equals("lecture")){
    			
    			roomView.invalidate();
    			RoomVisualization.roomState = 0;
    			roomView.draw(new Canvas());
    		}
    	}
    }
}
